package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class BibliotecaApp {

    private BufferedReader inputDataBuffer;
    private StartUp startUp;
    public ArrayList<Book> bookList = new ArrayList<Book>();
    public ArrayList<Movie> movieList = new ArrayList<Movie>();

    public BibliotecaApp(StartUp startUp, BufferedReader inputDataBuffer) {
        this.startUp = startUp;
        this.inputDataBuffer = inputDataBuffer;
        bookList.add(new Book("Head First Java"));
        bookList.add(new Book("Hamlet"));
        movieList.add(new Movie("The Sixth Sense", 1999, "M. Night Shyamalan", 7));
        movieList.add(new Movie("Vertigo", 1958, "Alfred Hitchcock", 8));
        movieList.add(new Movie("The Mist", 2007, "Frank Darabont", 7));
        movieList.add(new Movie("Porco Rosso", 1992, "Hayao Miyazaki", 9));
        movieList.add(new Movie("Hunger Games", 2012, "Gary Ross", 9));
        movieList.add(new Movie("Prometheus", 2012, "Ridley Scott", 3));
        movieList.add(new Movie("Happy Feet", 2006, "Warren Coleman", 4));
    }

    public String readUserInput() throws IOException {
        return inputDataBuffer.readLine();
    }

    public void run() throws IOException {
        startUp.welcome();
        startUp.menuOptions();
        runThroughMenu(readUserInput());
    }

    public void reserveBook() throws IOException {

        int bookChoice = Integer.parseInt(this.readUserInput());

        if (bookChoice <= bookList.size() && bookChoice > 0) {
            bookList.get(bookChoice - 1).reserve();
            startUp.thankyouForReservingBook();
        } else {
            startUp.bookDoesNotExist();
        }
    }

    public void runThroughMenu(String input) throws IOException {
        OptionInput[] values = OptionInput.values();
        for (OptionInput option : values) {
            if (option.optionNumber().equals(input)) {
                startUp.handleOption(option, bookList, movieList);
                if(input.equals("2")){
                    reserveBook();
                }
                return;
            }
        }
        startUp.printErrorMessage();
    }

    public static void main(String[] args) throws IOException {
        BibliotecaApp bibliotecaApp = new BibliotecaApp(new StartUp(System.out), new BufferedReader(new InputStreamReader(System.in)));
        bibliotecaApp.run();

    }
}
